﻿using System;
using System.Collections.Generic;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using HYOpeningCommon.DataEnum;
using HYOpeningCommon.OpeningEX;
using HYOpeningCommon.OpeningProfile;
using HYOpeningCommon.Public;
using HYOpeningCreate.Public;

namespace HYOpeningCommon.DbOpening
{
	public class DbCircleFloorOpening : DbFamilyOpening
	{
		public override bool CreateOpeningFamilyInstance(ViewScene ViewScene, out Element elem3D, out Element elem2D)
		{
			elem2D = null;
			elem3D = null;
			Autodesk.Revit.DB.View viewOpening2D = ViewScene.m_viewOpening2D;
			if (viewOpening2D == null)
			{
				elem3D = null;
				return false;
			}
			Document document = viewOpening2D.Document;
			OpeningXData openingXData = new OpeningXData();
			base.GetOpeningXDataBase(ref openingXData);
			Transaction transaction = new Transaction(document, "CreateOpeningFamilyInstance");
			try
			{
				FamilySymbolOperator familySymbolOperator = new FamilySymbolOperator(document, this);
				if (!familySymbolOperator.DoCalculate())
				{
					return false;
				}
				FamilySymbol familySymble = familySymbolOperator.GetFamilySymble();
				OpeningProfileCircle openingProfileCircle = this.m_profile as OpeningProfileCircle;
				transaction.Start();
				Floor floor = document.GetElement(this.m_strHostUniqueId) as Floor;
				Level level = document.GetElement(floor.LevelId) as Level;
				UnifiedModified.ActivateFamilySymbol(familySymble);
				FamilyInstance familyInstance = document.Create.NewFamilyInstance(openingProfileCircle.m_position, familySymble, floor, level, (Autodesk.Revit.DB.Structure.StructuralType)0);
				double num = 0.0;
				if (familySymbolOperator.IsNeedSetSize())
				{
					num = openingProfileCircle.m_dExtendDist * 2.0 + openingProfileCircle.m_dCutDiameter;
					familyInstance.GetParameter("洞口直径").Set(num);
				}
				openingXData.m_dHeight = (openingXData.m_dWidth = num);
				openingXData.m_eOpeningProfile = 0;
				openingXData.m_dMinZ = (openingXData.m_dMaxZ = openingProfileCircle.m_position.Z);
				openingXData.WriteOpeningXData(familyInstance);
				transaction.Commit();
				elem3D = familyInstance;
			}
			catch (Exception)
			{
				elem3D = null;
				transaction.RollBack();
				return false;
			}
			return true;
		}

		public override bool LoadOpeningFamily(Autodesk.Revit.DB.Document doc, ref FamilySymbol familySymbol)
		{
			base.SetFamilyName("楼板洞口-圆形");
			return base.LoadOpeningFamily(doc, ref familySymbol);
		}

		public override bool IsNeedCreateFamily()
		{
			return false;
		}

		public override bool CreateOpeningPreviewInstance(out Element elemPreview, PreViewScene preViewScene)
		{
			OpeningProfileCircle openingProfileCircle = this.m_profile as OpeningProfileCircle;
			XYZ position = openingProfileCircle.m_position;
			XYZ basisZ = XYZ.BasisZ;
			double num = 1.0;
			double radius = openingProfileCircle.m_dCutDiameter * num / 2.0;
			Transaction transaction = new Transaction(preViewScene.m_doc, "CreateModelGroup");
			bool result;
			try
			{
				XYZ origin = position + basisZ * preViewScene.m_offset;
				Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(basisZ, origin);
				if (plane == null)
				{
					elemPreview = null;
					result = false;
				}
				else
				{
					List<ElementId> list = new List<ElementId>();
					if (!CommonInterface.CreateModelCurve(radius, preViewScene.m_doc, plane, ref list, preViewScene.m_modelLineElemId))
					{
						elemPreview = null;
						result = false;
					}
					else
					{
						XYZ origin2 = position + basisZ.Negate() * (this.m_dHostWidth + preViewScene.m_offset);
						plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(basisZ.Negate(), origin2);
						if (plane == null)
						{
							elemPreview = null;
							result = false;
						}
						else if (!CommonInterface.CreateModelCurve(radius, preViewScene.m_doc, plane, ref list, preViewScene.m_modelLineElemId))
						{
							elemPreview = null;
							result = false;
						}
						else
						{
							transaction.Start();
							elemPreview = preViewScene.m_doc.Create.NewGroup(list);
							transaction.Commit();
							result = true;
						}
					}
				}
			}
			catch (Exception)
			{
				transaction.RollBack();
				elemPreview = null;
				result = false;
			}
			return result;
		}

		public override bool Equals(object obj)
		{
			return base.Equals(obj);
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Export2XmlNode(XmlElement xmlNode)
		{
			bool result;
			try
			{
				if (!base.Export2XmlNode(xmlNode))
				{
					result = false;
				}
				else
				{
					result = true;
				}
			}
			catch
			{
				xmlNode = null;
				result = false;
			}
			return result;
		}

		public override bool ImportByXmlNode(XmlNode xmlNode)
		{
			try
			{
				if (!base.ImportByXmlNode(xmlNode))
				{
					return false;
				}
			}
			catch
			{
				return false;
			}
			return true;
		}

		public override void AddSharedParameters(Element elem)
		{
			Transaction transaction = new Transaction(elem.Document, "AddSharedParameter");
			try
			{
				transaction.Start();
				base.AddSharedParameters(elem);
				Parameter parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningMajor);
				if (parameter != null && StrSpecialityObj.m_strSpecialityObj.GetLength(0) > (int)this.m_eSpeciality)
				{
					parameter.Set(StrSpecialityObj.m_strSpecialityObj[(int)this.m_eSpeciality]);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningSize);
				if (parameter != null)
				{
					string text = "φ" + this.GetOpeningSize(false);
					parameter.Set(text);
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		public override void GetProfileOutLine3D(ref List<XYZ> lstOutLineView3DPt, ref Transform transform, bool isAddExtend, XYZ location = null)
		{
			List<XYZ> list = new List<XYZ>();
			this.m_profile.GetProfileOutLineOnXOY(ref list, isAddExtend);
			if (transform == null)
			{
				transform = Transform.CreateRotation(XYZ.BasisZ, 0.0);
				transform.BasisY = XYZ.BasisY;
				transform.BasisZ = XYZ.BasisZ;
				transform.BasisX = XYZ.BasisX;
			}
			transform.Origin = this.m_profile.m_position;
			foreach (XYZ xyz in list)
			{
				lstOutLineView3DPt.Add(transform.OfPoint(xyz));
			}
		}
	}
}
